﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Data;
using System.Collections;
using System.Reflection;
using System.Globalization;

namespace SapB1WebApp.Utils
{
    public class Utils
    {
        public const string DATE = "dd/MM/yyyy";
        public const string SAP_FORMAT_DATE = "yyyyMMdd";
        public static DataTable ToDataTable<T>(List<T> items)
        {
            DataTable dataTable = new DataTable(typeof(T).Name);

            //Get all the properties
            PropertyInfo[] Props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
            foreach (PropertyInfo prop in Props)
            {
                //Setting column names as Property names
                dataTable.Columns.Add(prop.Name);
            }
            foreach (T item in items)
            {
                var values = new object[Props.Length];
                for (int i = 0; i < Props.Length; i++)
                {
                    //inserting property values to datatable rows
                    values[i] = Props[i].GetValue(item, null);
                }
                dataTable.Rows.Add(values);
            }
            //put a breakpoint here and check datatable
            return dataTable;
        }
        public static bool IsNumberInteger(string validateText)
        {
            try
            {
                int.Parse(validateText);
                return true;

            }
            catch
            {
                return false;
            }
        }
        public static bool IsNumber(string validateText)
        {
            try
            {
                double.Parse(validateText);
                return true;
            }
            catch { return false; }
        }
        public static DataSet IListToDataSet<T>(IList<T> list)
        {
            if (list == null || list.Count <= 0)
            {
                return null;
            }

            DataSet ds = new DataSet();
            DataTable dt = new DataTable(typeof(T).Name);
            DataColumn column;
            DataRow row;

            System.Reflection.PropertyInfo[] myPropertyInfo = typeof(T).GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);

            foreach (T t in list)
            {
                if (t == null)
                {
                    continue;
                }
                row = dt.NewRow();
                for (int i = 0, j = myPropertyInfo.Length; i < j; i++)
                {
                    System.Reflection.PropertyInfo pInfo = myPropertyInfo[i];
                    string name = pInfo.Name;
                    if (dt.Columns[name] == null)
                    {
                        column = new DataColumn(name, Nullable.GetUnderlyingType(pInfo.PropertyType) ?? pInfo.PropertyType);
                        //dt.Columns.Add(column);
                    }

                    row[name] = pInfo.GetValue(t, null) ?? DBNull.Value;
                }

                dt.Rows.Add(row);
            }
            ds.Tables.Add(dt);
            return ds;
        }
        public static DataTable IlistToDataTable(List<string> fields, IList iList)
        {
            DataTable dt = new DataTable();

            foreach (string fieldName in fields)
            {
                dt.Columns.Add(fieldName);
            }


            foreach (object[] values in iList)
            {
                DataRow row = dt.NewRow();
                for (int i = 0; i < fields.Count; i++)
                {
                    row[i] = values[i];
                }
                dt.Rows.Add(row);
            }

            return dt;
        }
        [MethodImpl(MethodImplOptions.NoInlining)]
        public static string GetCurrentMethod()
        {
            StackTrace st = new StackTrace();
            StackFrame sf = st.GetFrame(1);

            return sf.GetMethod().Name;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="strDate"></param>
        /// <returns></returns>
        public static DateTime ConvertToDate(string strDate)
        {
            return DateTime.ParseExact(strDate, Utils.DATE, null, System.Globalization.DateTimeStyles.None);
        }
        public static DataTable ReturnMessage(int ErrCode, string ErrMsg)
        {
            DataTable dtMessage = new DataTable();
            dtMessage.Columns.Add("ErrCode", typeof(int));
            dtMessage.Columns.Add("ErrMsg", typeof(string));

            DataRow dr = dtMessage.NewRow();
            dr["ErrCode"] = ErrCode;
            dr["ErrMsg"] = ErrMsg;
            dtMessage.Rows.Add(dr);

            return dtMessage;
        }
    }
}